package models

import (
	"fmt"
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql" // import your used driver
	"time"
)

type NoteBook struct {
	Id       int64 `orm:"column(id);pk"`
	Username string    // Username=> username
	Passwd   string // PassWd=> pass_wd
	Sex      string
	Content  string
	Creatdt  string `json:"creatdt",orm:"creatdt"` //后面这串代码表示改变原有规则，防止出现大小写和‘_’问题
	Logintime  string    // LoginTime=> login_time

}

/*
NoteBook的 sql操作
*/

/**
 * id查询一个用户
 */
func GetNoteById(id int64) *NoteBook {
	o := orm.NewOrm()
	noteBook := new(NoteBook)
	//var noteBook *NoteBook =  new(NoteBook)

	noteBook.Id = id

	err := o.Read(noteBook)

	if err == orm.ErrNoRows {
		fmt.Println("查询不到")
	} else if err == orm.ErrMissPK {
		fmt.Println("找不到主键")
	} else {
		fmt.Println(noteBook.Id, noteBook.Username)
	}

	return noteBook
}
/**
 * 增加用户
 */
func NoteAdd(name, sex, passwd, content string) (insertId int64, err error) {
	o := orm.NewOrm()
	timeNow := time.Now()
	nowTime := timeNow.Format("2006-01-02 15:04:05")
    noteBook := NoteBook{}
    noteBook.Username = name
	noteBook.Passwd = passwd
	noteBook.Sex = sex
	noteBook.Content = content
	noteBook.Creatdt = nowTime
	noteBook.Logintime = nowTime
	//user := NoteBook{Username: name, Passwd: passwd, Sex: sex, Content: content}
	id, err := o.Insert(&noteBook)
	fmt.Println(id)
	//return user
	return id, err
}

/**
 * 获取；评论列表
 */
func GetNoteList(offset, limit int) ([]NoteBook, error) {

	var NoteBooks []NoteBook
	_, err := NoteBookObj().Offset(offset).Limit(limit).All(&NoteBooks)
	if err != nil {
		return nil, err
	}

	return NoteBooks, nil
}
/**
 * 获取：总数
 */
func GetNoteCount()  int64{
	o := orm.NewOrm()
	noteBook := new(NoteBook)
	cnt, err := o.QueryTable(noteBook).Count()
	if err != nil {
		fmt.Printf(err.Error())
	}

	return cnt
}

/**
 * 根据ID删除数据
 */
func DelNoteById(id int64) (int64, error) {

	o := orm.NewOrm()
	noteBook := new(NoteBook)
	noteBook.Id = id

	num, err := o.Delete(noteBook)
	if err != nil {
		return 0, err
	}

	return num, nil
}

/**
 * 根据ID修改数据
 */
func UpMessage(id int64, name, sex, passwd, content string) *NoteBook {
	o := orm.NewOrm()
	noteBook := new(NoteBook)
	noteBook.Id = id
	//fmt.Println(noteBook.Id)
	if o.Read(noteBook) == nil {
		noteBook.Username = name
		noteBook.Sex = sex
		noteBook.Passwd = passwd
		noteBook.Content = content
		//fmt.Println(noteBook.Username)
		if num, err := o.Update(noteBook, "Username", "Passwd", "Sex", "Content"); err == nil {
			fmt.Println(num)

		}
	}

	//return user
	return noteBook

}

///**
// *  用户验证
// */
//
//func InUser(usname,passwd string) (*NoteBook,error) {
//	/*
//	//方法一
//	noteBook := new(NoteBook)
//	noteBook.Username = usname
//	noteBook.Passwd = passwd
//
//	err := o.Read(noteBook,"Username","Passwd")
//
//	if err!=nil{
//		fmt.Println("query error:"+err.Error())
//	    return err
//	}
//*/
// //方法二
//	var noteBook *NoteBook = new(NoteBook)
//	o := orm.NewOrm()
//	qs := o.QueryTable(noteBook) // 返回 QuerySeter
//	qs = qs.Filter("Username", usname) // WHERE id = 1
//	qs = qs.Filter("Passwd", passwd) // WHERE id = 1
//	err := qs.One(noteBook)
//	_,err2 := qs.All(noteBook,"Id")
//	if err2 != nil {
//		log := UitlLog()
//		log.Error("connect db err"+err.Error())
//		return nil, err2
//	}
//	if err!=nil{
//		fmt.Println("query error:"+err.Error())
//
//		return  nil,err
//	}
//	//if err == orm.ErrMultiRows {
//	//	// 多条的时候报错
//	//	fmt.Printf("Returned Multi Rows Not One"+err.Error())
//	//	return err
//	//}
//	if err == orm.ErrNoRows {
//		// 没有找到记录
//		fmt.Printf("Not row found"+err.Error())
//		return  nil,err
//	} else {
//
//	timeNow := time.Now()
//	loginTime := timeNow.Format("2006-01-02 15:04:05")
//	//fmt.Println("时间:"+loginTime)
//	noteBook.Logintime = loginTime
//
//	if _, err := o.Update(noteBook); err != nil {
//		fmt.Println("更新失败：",err.Error())
//
//	 }
//	}
//	// 成功
//	return  noteBook,nil
//
//}

/**
 *  Notebook 表实例化
 */
func NoteBookObj() orm.QuerySeter {
	return orm.NewOrm().QueryTable(new(NoteBook))
}
